package com.karst.essentials.chapter02
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	internal class FirstApplication extends Sprite
	{
		public function FirstApplication()
		{
			super();
			createView() ;
			addEventListener( Event.ENTER_FRAME, renderHandler ) ;
		}

		private var scene : Scene3D = null ;
		private var camera : Camera3D = null ;
		private var viewport : Viewport3D = null ;
		private var renderEngine : BasicRenderEngine = null ;
		private var sphere : Sphere = null ;
		private function createView() : void
		{
			if( scene == null )
				scene = new Scene3D() ;
			if( camera == null )
				camera = new Camera3D() ;
			if( viewport == null )
				viewport = new Viewport3D() ;
			if( renderEngine == null )
				renderEngine = new BasicRenderEngine() ;
			
			if( sphere == null )
				sphere = new Sphere() ;
			addChild( viewport ) ;
			scene.addChild( sphere ) ;
		}
		
		
		private function renderHandler( event : Event ) : void
		{
			if( sphere )
				sphere.localRotationY-- ;
			if( renderEngine && scene && camera && viewport )
				renderEngine.renderScene( scene, camera, viewport ) ;
		}
	}
}